/**
 * 上传文件按钮
 */
define(["app"], function (app) {

    var deps = ["$parse", "$timeout"];

    function directive ($parse, $timeout) {

        return {
            restrict: "A",
            link: function (scope, element, attrs) {
                var fileInput = $("<input type='file' name='fileinput' class='fileinput fc-common-upload-btn' >");
                if (attrs.isMul == "true") {
                    fileInput = $("<input type='file' name='fileinput' class='fileinput fc-common-upload-btn' multiple>");
                }

                fileInput.insertAfter(element);

                fileInput.bind("change", function () {
                    var input = this;
                    var files = [];

                    for (var i = 0; i < input.files.length; i++) {
                        files.push(input.files[i]);
                    }

                    $timeout(function () {
                        //上传的文件保存到父scope
                        $parse(attrs.jssFileInput).assign(scope, files);
                    });
                });
            }
        };
    }

    directive.$inject = deps;
    app.lazy.directive("jssFileInput", directive);
});